//
//  BKCustextFieldView.m
//  bestkeep
//
//  Created by windCould on 16/11/28.
//  Copyright © 2016年 utouu. All rights reserved.
//

#import "BKCustextFieldView.h"
#import "BKMyCommon.h"


@interface BKCustextFieldView ()

@property (nonatomic,strong)UIButton * imgCodeBtn;
@property (nonatomic,strong)UIView * bottomLine;

@end



@implementation BKCustextFieldView


-(instancetype)initWithFrame:(CGRect)frame style:(BKCustextFieldViewType)style
{
    self = [super initWithFrame:frame];
    if (self) {
    
        [self setUpViewsWithStyle:style];
    }
    
    return  self;
}

-(void)setImgUrlStr:(NSString *)imgUrlStr
{
    _imgUrlStr = imgUrlStr;
    
    UIView * spaceView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 40, 21)];
    spaceView.backgroundColor = [UIColor whiteColor];
    
    UIImageView * leftViewV = [[UIImageView alloc]init];
    if ([imgUrlStr isEqualToString:@"BK_My_moblie_Message"] || [imgUrlStr isEqualToString:@"BK_My_imgcode"]) {
        
         leftViewV.contentMode = UIViewContentModeScaleAspectFit;
    } else {
    
         leftViewV.contentMode =  UIViewContentModeCenter;   
    }
    
    leftViewV.frame = CGRectMake(10, 0, 21, 21);
    [spaceView addSubview:leftViewV];
    leftViewV.image = [UIImage imageNamed:imgUrlStr];
    self.textField.leftViewMode = UITextFieldViewModeAlways;
    self.textField.leftView = spaceView;
}

-(void)setPlaceHolderString:(NSString *)placeHolderString
{
    self.textField.placeholder = placeHolderString;
}

-(void)setRightImg:(UIImage *)rightImg
{
    _rightImg = rightImg;
    _imgCodeBtn.imageView.contentMode = UIViewContentModeScaleAspectFit;
    [_imgCodeBtn setImage:rightImg forState:UIControlStateNormal];
}

#pragma mark =========================Function===================================

-(void)sendSmsInformation
{
    _countDownBtn.btnSelect = YES;
    if ([self.myDlegate respondsToSelector:@selector(custextFieldView:rightButtonTouch:)]) {
     
        [self.myDlegate custextFieldView:self rightButtonTouch:_countDownBtn];
    }
}

-(void)getImgCodeBtnClick
{
    if ([self.myDlegate respondsToSelector:@selector(custextFieldView:imgCodeButtonTouch:)]) {
        
        [self.myDlegate custextFieldView:self imgCodeButtonTouch:_imgCodeBtn];
    }
}

//视图布局
-(void)setUpViewsWithStyle:(BKCustextFieldViewType)style
{
    _textField = [[UITextField alloc]initWithFrame:CGRectZero];
    _textField.font = [UIFont systemFontOfSize:16];
    _textField.textColor = HexColor(0x5f646e);
    _textField.clearButtonMode = UITextFieldViewModeWhileEditing;
    _textField.returnKeyType = UIReturnKeyDefault;
    [self addSubview:_textField];
    
    _bottomLine = [[UIView alloc]initWithFrame:CGRectZero];
    _bottomLine.backgroundColor = RGBCOLOR(210, 210, 210, 1);
    [self addSubview:_bottomLine];
    
    _countDownBtn = [[BKUserCustomBtn alloc]initWithFrame:CGRectZero];
    [_countDownBtn addTarget:self action:@selector(sendSmsInformation) forControlEvents:UIControlEventTouchUpInside];
    _countDownBtn.hidden = style == BKCustextFieldViewTimeCountDownType ? NO:YES;
    [self addSubview:_countDownBtn];
    
    _imgCodeBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [_imgCodeBtn addTarget:self action:@selector(getImgCodeBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self addSubview:_imgCodeBtn];
    _imgCodeBtn.hidden = style == BKCustextFieldViewImageCodeType ? NO:YES;
    
    [self layoutViewsWithStyle:style];
    
}

-(void)layoutViewsWithStyle:(BKCustextFieldViewType)style
{
    
    [_textField mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(self.mas_top).offset(0);
        make.left.mas_equalTo(self.mas_left).offset(0);
        
        if (style == BKCustextFieldViewTimeCountDownType) {
            
            make.right.mas_equalTo(self.mas_right).offset(-83);
        } else if (style == BKCustextFieldViewImageCodeType ){
        
            make.right.mas_equalTo(self.mas_right).offset(-120);
            
        }else {
        
            make.right.mas_equalTo(self.mas_right).offset(0);
        }
        
        make.bottom.mas_equalTo(self.mas_bottom).offset(-0.5);
    }];
    
    [_countDownBtn mas_makeConstraints:^(MASConstraintMaker *make) {
       
        if (style == BKCustextFieldViewTimeCountDownType) {
            
            make.centerY.mas_equalTo(self.mas_centerY).offset(0);
            make.right.mas_equalTo(self.mas_right).offset(-3);
            make.size.mas_equalTo(CGSizeMake(73, 23));
        } else {
            
            make.top.mas_equalTo(self.mas_top).offset(0);
            make.left.mas_equalTo(self.mas_left).offset(0);
            make.size.mas_equalTo(CGSizeMake(0, 0));
        }
    }];
    
    [_imgCodeBtn mas_makeConstraints:^(MASConstraintMaker *make) {
       
        if (style == BKCustextFieldViewImageCodeType) {
            
            make.top.mas_equalTo(self.mas_top).offset(0);
            make.left.mas_equalTo(self.textField.mas_right).offset(40);
            make.right.mas_equalTo(self.mas_right).offset(0);
            make.bottom.mas_equalTo(self.mas_bottom).offset(0);
        } else {
        
            make.top.mas_equalTo(self.mas_top).offset(0);
            make.left.mas_equalTo(self.mas_left).offset(0);
            make.size.mas_equalTo(CGSizeMake(0, 0));
        }
        
    }];
    
    
    [_bottomLine mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.height.mas_equalTo(@(0.5));
        make.left.mas_equalTo(self.mas_left).offset(0);
//        if (style == BKCustextFieldViewImageCodeType) {
//            
//            make.width.mas_equalTo(_textField.mas_width);
//        } else {
        
            make.right.mas_equalTo(self.mas_right).offset(0);
//        }
        make.bottom.mas_equalTo(self.mas_bottom).offset(0);
    
    }];
    
}



@end
